<?php

namespace app\index\library\traits;

use app\common\library\Email;
use think\Cookie;

trait Pass
{
    public function forgetPassword1()
    {
        return $this->view->fetch('user/pass/forget_password1');
    }
    public function forgetPassword2()
    {
        if ($this->request->isPost()) {
            $tel = $this->request->post('tel');
            session('forgetPassword2', $tel);
            $this->view->assign('tel', $tel);
            return $this->view->fetch('user/pass/forget_password2');

        }
    }
    public function forgetPassword3()
    {
        if ($this->request->isPost()) {
            $repass = md5($this->request->post('repass'));
            $model = new \app\admin\model\user\User;
            if ($res = $model->where('tel', session('forgetPassword2'))->find()) {
                $res->password = $repass;
                $res->save();
                return $this->view->fetch('user/pass/forget_password4');
            }
        }
        return $this->view->fetch('user/pass/forget_password3');
    }
    public function forgetPassword4()
    {
        if ($this->request->isPost()) {
            $pass = md5($this->request->post('pass'));
            $model = new \app\admin\model\user\User;
            if ($res = $model->where('email', session('forgetEmailPassword2'))->find()) {
                $res->password = $pass;
                if ($res->save()) {
                    return $this->view->fetch('user/pass/forget_password4');
                }
            }
            $this->error('重置密码失败');
        }
    }
    public function forgetEmailPassword1()
    {
        if ($this->request->isPost()) {
            $email = $this->request->post('email');
            $model = new \app\admin\model\user\User;
            if ($res = $model->where('email', $email)->find()) {
                $token      = md5(time());
                $res->token = $token;
                if ($res->save()) {
                    $url        = $this->request->domain().'/index/user/forgetEmailPassword2/email/'.$email.'/token/'.$token;
                    $emailObj   = new Email;
                    $content    = '<p>请点击<a href="'.$url.'">链接</a>找回密码</p>';
                    $emailObj->to($email)
                        ->subject('【智识医疗】- 找回密码')
                        ->message($content)
                        ->send();
                    $this->assign('email', $email);
                    return $this->view->fetch('user/pass/forget_email_password2');
                }
            }
            $this->error('邮箱不存在，请重新输入');

        }
        return $this->view->fetch('user/pass/forget_email_password1');
    }
    public function forgetEmailPassword2($email = null, $token = null)
    {
        if ($token && $email) {
            $model = new \app\admin\model\user\User;
            if ($res = $model->where(['email'=>$email, 'token'=>$token])->find()) {
                session('forgetEmailPassword2', $email);
                return $this->forgetPassword3();
            }
        }
    }

    public function editPassword()
    {
        if ($this->request->isPost()) {
            $pass = md5($this->request->post('pass'));
            $repass = md5($this->request->post('repass'));
            $model = new \app\admin\model\user\User;
            if ($res = $model->where(['id'=>$this->userID, 'password'=>$pass])->find()) {
                $res->password = $repass;
                if ($res->save()) {
                    $this->success('密码修改成功', '/');
                }
                $this->error('密码修改失败');
            }
            $this->error('您输入的原密码不正确，请重新输入');
        }
        return $this->view->fetch('user/pass/edit_password');
    }

    public function checkPassword()
    {
        if ($this->request->isAjax()) {
            $pass = md5($this->request->post('pass'));
            $model = new \app\admin\model\user\User;
            if ($res = $model->where(['id'=>$this->userID, 'password'=>$pass])->find()) {
                $this->success();
            }
            $this->error();
        }
    }

    /**
     * 注册
     * @return string
     * @throws \think\Exception
     */
    public function register()
    {
        return $this->view->fetch('user/register/register');
    }

    public function registerSuccess()
    {
        if ($this->request->isPost()) {
            if (session('code') == $this->request->post('code')) {
                $model = new \app\admin\model\user\User;
                $data['tel'] = $this->request->post('tel');
                $data['email'] = $this->request->post('email');
                $data['cpn'] = $this->request->post('cpn');
                $data['password'] = md5($this->request->post('repass'));
                $data['username'] = $data['tel'];
                if (!$model->where('tel', $data['tel'])->whereOr('email', $data['email'])->find()) {
                    $model->save($data);
                    $third = Cookie::get('medical_third');
                    if ($third) {
                        Third::update(['user_id' => $model->id], ['platform' => $third['platform'], 'openid' => $third['openid']]);
                    }
                } else {
                    $this->error('手机号或邮箱已被注册');
                }
            } else {
                $this->error('验证码不正确');
            }

            return $this->view->fetch('user/register/register_success');
        }
    }

    public function relevance()
    {
        $model = new \app\admin\model\user\User;
        $third = Cookie::get('medical_third');

        if (!empty($third['user_id'])) {
            if ($res = $model::get($third['user_id'])) {
                session('medical_user', $res);
                $this->success('登录成功', '/index/user/index');
            }
        }
        return $this->view->fetch('user/register/relevance');

    }

    public function relevanceAccount()
    {
        if ($this->request->isPost()) {
            if (session('code') == $this->request->post('code')) {
                $model = new \app\admin\model\user\User;
                $data['tel'] = $this->request->post('tel');
                if ($user = $model->where($data)->find()) {
                    session('medical_user', $user);
                    $third = Cookie::get('medical_third');
                    Third::update(['user_id' => $user['id']], ['platform' => $third['platform'], 'openid' => $third['openid']]);
                    $this->success('绑定成功！', '/');
                }
                $this->error('用户不存在，请注册');
            } else {
                $this->error('验证码不正确');
            }
        }
        return $this->view->fetch('user/register/relevance_account');
    }

    public function login()
    {
        if ($this->request->isPost()) {
            $username = $this->request->post('username');
            $password = md5($this->request->post('password'));

            $model = new \app\admin\model\user\User;
            $where = [
                'username|tel|email' => $username,
                'password' => $password
            ];
            if ($user = $model->where($where)->find()) {
                session('medical_user', $user);
                $this->success('登录成功');
            } else {
                $this->error('用户名或密码错误');
            }
        }
    }

    public function loginOut()
    {
        session('medical_user', null);
        $this->success('已退出', '/');
    }
}
